from rest_framework import serializers

from database.models import *
from utils.unite.serializer_helper import ChoiceDisplayField
from utils.util_datetime import *


# fruit = serializers.ChoiceField(choices=['Apple', 'Banana', 'Orange'],
#                                 default='Apple', help_text="Fruit selection")

class WxLoginSerializer(serializers.ModelSerializer):
    username = serializers.CharField(label='账号',
                                     required=True,
                                     error_messages={
                                         'required': '请上传账号',
                                         'blank': '账号不可以为空',
                                     },
                                     )

    class Meta:
        model = User
        fields = (
            'username',
        )


class GetGoodsCategoryListSerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory
        fields = (
            'id',
            'name',
        )


class GetGoodsListSerializer(serializers.ModelSerializer):
    datetime_created = serializers.DateTimeField(format=DATETIME_FORMAT_YMD_HM)

    class Meta:
        model = Goods
        fields = (
            'uuid',
            'datetime_created',
            'name'
        )


class GetGoodsDetailSerializer(serializers.ModelSerializer):
    datetime_created = serializers.DateTimeField(format=DATETIME_FORMAT_YMD_HM)
    content = serializers.SerializerMethodField()
    unit_name = serializers.SerializerMethodField()

    class Meta:
        model = Goods
        fields = (
            'uuid',
            'datetime_created',
            'name',
            'price',
            'unit_name',
            'unit_number',
            'content',
        )

    def get_content(self, obj):
        if hasattr(object, 'goods_article'):
            article = obj.goods_article
            if article is not None:
                return obj.goods_article.content
        return ""

    def get_unit_name(self, obj):
        if obj.unit is not None:
            return obj.unit.get_type_display()
        return ""


class GetBusinessActivityListSerializer(serializers.ModelSerializer):
    class Meta:
        model = BusinessActivity
        fields = (
            'pk',
            'name',
        )


class GetBusinessActivityDetailSerializer(serializers.ModelSerializer):
    serializer_choice_field = ChoiceDisplayField

    class Meta:
        model = BusinessActivity
        fields = (
            'pk',
            'name',
            'amount',
            'quota',
            'datetime_start',
            'datetime_end',
            'content',
        )


class GetBusinessNoticeListSerializer(serializers.ModelSerializer):
    class Meta:
        model = BusinessNotice
        fields = (
            'pk',
            'title',
        )


class GetBusinessNoticeDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = BusinessNotice
        fields = (
            'pk',
            'title',
            'content',
        )
